//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.未分类.demo1AND20;

public class demo13 {
    public demo13() {
    }

    public static void main(String[] args) {
        System.out.println(romanToInt("MMCCCXCIX"));
    }

    public static int romanToInt(String s) {
        StringBuilder sb = new StringBuilder(s);
        int result = 0;
        String[][] f = new String[][]{{"IV", "IX"}, {"XL", "XC"}, {"CD", "CM"}};
        String[][] roman = new String[][]{{"I", "V"}, {"X", "L"}, {"C", "D"}, {"M"}};
        int index = 0;

        while(!sb.isEmpty()) {
            if (sb.length() >= 2 && index != 3 && sb.substring(sb.length() - 2).equals(f[index][0])) {
                result += 4 * (int)Math.pow((double)10.0F, (double)index);
                sb.deleteCharAt(sb.length() - 1);
                sb.deleteCharAt(sb.length() - 1);
                ++index;
            } else if (sb.length() >= 2 && index != 3 && sb.substring(sb.length() - 2).equals(f[index][1])) {
                result += 9 * (int)Math.pow((double)10.0F, (double)index);
                sb.deleteCharAt(sb.length() - 1);
                sb.deleteCharAt(sb.length() - 1);
                ++index;
            } else if (!sb.isEmpty() && index != 3 && sb.substring(sb.length() - 1).equals(roman[index][1])) {
                result += 5 * (int)Math.pow((double)10.0F, (double)index);
                sb.deleteCharAt(sb.length() - 1);
                ++index;
            } else {
                for(int i = 0; i < 4 && !sb.isEmpty(); ++i) {
                    if (sb.substring(sb.length() - 1).equals(roman[index][0])) {
                        result += (int)Math.pow((double)10.0F, (double)index);
                        sb.deleteCharAt(sb.length() - 1);
                    } else if (sb.substring(sb.length() - 1).equals(roman[index][1])) {
                        result += 5 * (int)Math.pow((double)10.0F, (double)index);
                        sb.deleteCharAt(sb.length() - 1);
                    }
                }

                ++index;
            }
        }

        return result;
    }
}
